package com.likai.authority.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpStatusCodeException;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.likai.authority.pojo.Authority;
import com.likai.authority.pojo.Role;
import com.likai.authority.pojo.TreeNode;
import com.likai.authority.pojo.User;
import com.likai.authority.service.IAuthorityService;
import com.likai.authority.service.IRoleService;
import com.likai.authority.service.IUserService;
import com.likai.authority.service.impl.UserServiceImpl;
import com.likai.authority.util.AnswerInfo;
import com.likai.authority.util.PageList;
import com.likai.authority.util.StringUtil;

/**
 * authorityController层
 * @author likai
 * @since 2018-04-02
 */
@RestController
public class AuthorityController {
	
	@Autowired
	@Qualifier("authorityService")
	private IAuthorityService authorityService ;
	
	@Autowired
	@Qualifier("roleService")
	private IRoleService roleService ;
	
	@Autowired
	@Qualifier("userService")
	private IUserService userService ;
	/**
	 * 获取树菜单
	 * @param parentId
	 * @return
	 */
	@RequestMapping(value="/menu",method=RequestMethod.GET)
	public ResponseEntity<List<TreeNode>> menu(
			String parentId,
			HttpServletRequest request
			) {
		User user = (User) request.getSession().getAttribute("user") ;
		//根据用户的roleId查询authIds
		Role role = roleService.findByRoleId(user.getRoleid()) ;
		String [] authIds = role.getAuthids().split(",") ;
		List<TreeNode> nodeList = authorityService.findMenu(parentId,authIds) ;
		if(nodeList == null || nodeList.size() == 0) {
			return new ResponseEntity<List<TreeNode>>(HttpStatus.NOT_FOUND) ;
		}
		return new ResponseEntity<List<TreeNode>>(nodeList,HttpStatus.OK) ;
	}
	
	@RequestMapping(value="/sqMenu",method=RequestMethod.GET)
	public ResponseEntity<List<TreeNode>> sqMenu(
			String parentId,
			String roleId,
			HttpServletRequest request
			) {
		//根据用户的roleId查询authIds
		Role role = roleService.findByRoleId(roleId) ;
		String [] authIds = role.getAuthids() == null ? null: role.getAuthids().split(",") ;
		List<TreeNode> nodeList = authorityService.findSqMenu(parentId,authIds) ;
		if(nodeList == null || nodeList.size() == 0) {
			return new ResponseEntity<List<TreeNode>>(HttpStatus.NOT_FOUND) ;
		}
		return new ResponseEntity<List<TreeNode>>(nodeList,HttpStatus.OK) ;
	}
	
	@RequestMapping(value="/treegrid",method=RequestMethod.GET)
	public ResponseEntity<List<TreeNode>> treegrid(
			String parentId,
			HttpServletRequest request
			) {
		List<TreeNode> nodeList = authorityService.findTreeGrid(parentId) ;
		if(nodeList == null || nodeList.size() == 0) {
			return new ResponseEntity<List<TreeNode>>(HttpStatus.NOT_FOUND) ;
		}
		return new ResponseEntity<List<TreeNode>>(nodeList,HttpStatus.OK) ;
	}
	
	@RequestMapping(value="/roleList",method=RequestMethod.GET)
	public ResponseEntity<List<Role>> roleList() {
		List<Role> list = roleService.findList(null) ;
		if(list == null || list.size() == 0) {
			return new ResponseEntity<List<Role>>(HttpStatus.NO_CONTENT) ;
		} else {
			return new ResponseEntity<List<Role>>(list,HttpStatus.OK) ;
		}
	}
	
	@RequestMapping(value="/rolepageList",method=RequestMethod.GET)
	public ResponseEntity<PageList> rolepageList(
			String rolename,
			Integer page,
			Integer rows
			) {
		Role po = new Role() ;
		if(StringUtil.isNotEmpty(rolename)) {
			po.setRolename(rolename);			
		}
		PageHelper.startPage(page, rows) ;
		List<Role> list = roleService.findList(po) ;
		PageInfo pageInfo = new PageInfo(list,5) ;
		PageList pageList = new PageList(
				pageInfo.getTotal(),
				pageInfo.getList()
				) ;
		return new ResponseEntity<PageList>(pageList,HttpStatus.OK) ;
		
	}
	
	@RequestMapping(value="/existUser",method=RequestMethod.GET)
	public ResponseEntity<AnswerInfo> existUser(
			String roleid
			) {
		AnswerInfo json = new AnswerInfo() ;
		List<User> list = userService.findByRoleId(roleid) ;
		if(list == null || list.size() == 0) {
			json.setData("false");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		} else {
			json.setData("true");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		}
	}
	
	@RequestMapping(value="/removeRole/{ids}",method=RequestMethod.DELETE)
	public ResponseEntity<AnswerInfo> removeRole(
			@PathVariable(value="ids") String [] ids
			) {
		AnswerInfo json = new AnswerInfo() ;
		int result = roleService.removeByIds(ids) ;
		if(result == 1) {
			json.setData("删除成功");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		} else {
			json.setData("删除失败");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		}
	}
	
	@RequestMapping(value="/updateRole",method=RequestMethod.POST)
	public ResponseEntity<AnswerInfo> updateRole(
			Role po
			) {
		AnswerInfo json = new AnswerInfo() ;
		int result = roleService.updateRole(po) ;
		if(result == 1) {
			json.setData("添加成功");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		} else {
			json.setData("添加失败");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		}
	}
	
	@RequestMapping(value="/vaildRoleName",method=RequestMethod.GET)
	public ResponseEntity<AnswerInfo> vaildRoleName(
			String rolename
			) {
		AnswerInfo json = new AnswerInfo() ;
		Role role = roleService.findByRolename(rolename) ;
		if(role == null) {
			json.setData("true");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		} else {
			json.setData("false");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		}
	}
	
	@RequestMapping(value="/sqRole",method=RequestMethod.POST)
	public ResponseEntity<AnswerInfo> sqRole(
			Role po 
			) {
		AnswerInfo json = new AnswerInfo() ;
		int result = roleService.updateRole(po) ;
		if(result == 1) {
			json.setData("授权成功");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		} else {
			json.setData("授权失败");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		}
	}
	
	@RequestMapping(value="/updateAuth",method=RequestMethod.POST)
	public ResponseEntity<AnswerInfo> updateAuth(
			Authority po
			) {
		AnswerInfo json = new AnswerInfo() ;
		int result = authorityService.updateAuth(po) ;
		if(result == 1) {
			json.setData("添加成功");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		} else {
			json.setData("添加失败");
			return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
		}
	}
	
	@RequestMapping(value="/removeAuth",method=RequestMethod.POST)
	public ResponseEntity<AnswerInfo> removeAuth(
			String authId,
			String parentId
			) {
		AnswerInfo json = authorityService.removeAuth(authId,parentId) ;
		return new ResponseEntity<AnswerInfo>(json,HttpStatus.OK) ;
	}
	
} 
